<?php

class Client_Table_Vacancy extends Apk_Db_Table
{
	protected $_name      = 'vacancy';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }
    
    public function GetDataList($page=1, $params=array())
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)      		      		  
    		  ->where($this->_name.'.is_active = 1')
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->order($this->_name.'.public_date desc');
    		  
    	// **  Params  **
    	if(count($params) > 0)	  
    	foreach ($params as $k => $v)
    	switch($k)
    	{
    		case 'cats':
    			$sel->where('id_cat in (?)', $v);
    			break;    	
    		default:	
    			$sel->where($this->_name.'.'.$k.' = ?', $v);
    	}    			
		
    		  
    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => 2));
    	 
    }
    
    public function GetLast($id_cat = null, $limit = 3)
    {    	
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'title', 'desc', 'public_date'))
    				->where('is_active')
    				->where('is_removed = 0')
    				->where('is_ghost = 0')    				    				
    				->order('public_date desc')
    				->limit($limit);    	

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }     
        

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)    				
		    		->where($this->_name.'.is_active')		    		
		    		->where($this->_name.'.is_removed = 0')
		    		->where($this->_name.'.is_ghost = 0')
    				->where($this->_name.'.id = ?', $id);       	
    				
		try {
	    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
		}
		catch (Exception $e)
		{
			return array();
		}		
    	
    	return $data;
    }           
   
    
    public function GetNextBack($id = null, $params=array())
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title'))   
    		  ->where($this->_name.'.is_ghost = 0')    		  
    		  ->where($this->_name.'.is_removed = 0')    		      		  
    		  ->where($this->_name.'.is_active')    		  
    		  ->order($this->_name.'.public_date desc');    
    		  
    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}     		  		  	  
    		  
    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();	  
    	
    	$ret = array();
    	$ret['count'] = count($data);
    	if($ret['count'] > 0)
    	foreach ($data as $k => $v)    	
    	if($v['id'] == $id)
    	{
    		$ret['pos'] = $k+1;
    		if($k != $ret['count']-1) $ret['next'] = $data[$k+1];
    		if($k != 0) $ret['back'] = $data[$k-1];    		
    	}
    	
    	return $ret;
    }    
    
    public function AddHit($id)
    {
    	$data = $this->GetDataRow($id);
    	
    	$d['hit_count'] = $data['hit_count'] + 1;
    	$this->update($d, 'id = '.$id);
    		  
    	return true;    		    	
    } 
    
    public function is_rated($id, $ip_addr)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_rating')
    		  ->where('ip_addr = ?', $ip_addr)
    		  ->where('id_article = ?', $id);
    		  
    	return $this->getList($sel)->count();
    }
    
    public function rating($data)
    {
    	if($this->is_rated($data['id_article'], $data['ip_addr'])) return false;
    	
    	$data['date'] = time();
    	
    	$this->getAdapter()->insert('articles_rating', $data);    	
    	$this->calc_rate($data['id_article']);
    	return true;
    }   
    
    public function calc_rate($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_rating', array('count' => 'count(id)', 'sum' => 'sum(value)'))
    		  ->where('id_article = ?', $id);
    		  
    	$data = $this->getList($sel)->getRow(0);
    	
    	$d['rating_count'] = $data->count;
    	if($data->count > 0) $d['rating'] = $data->sum / $data->count;
    	else $d['rating'] = 0;
    	    	
    	parent::update($d, 'id = '.$id);   	
    }
    
    public function comment($data)
    {    		  
    	$data['add_date'] = time();    	
    	$this->getAdapter()->insert('articles_comments', $data);   	
    	return $this->getAdapter()->lastInsertId('articles_comments');
    }     
    
    public function GetAccessList($a = null, $lang = 'ru')
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('access_level', array('id', 'title' => 'title_'.$lang))
					->order('title_ru');		
					
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    } 
      
    

    
    //**********************************************
    // insert/update/delete    
    
	public function insert(array $data, $user_id)
    {
    	$data['public_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;
    	$data['is_active'] = 1;
    	
        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id = 0)
    {
    	$data['modify_date'] = time();    	
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	//if(!isset($data['is_active'])) $data['is_active'] = 0;  	
    	//if(!isset($data['is_commented'])) $data['is_commented'] = 0;  	
    	//if(!isset($data['is_rated'])) $data['is_rated'] = 0;  	
    	
		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {    	
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id; 
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;
	    	
	    	parent::update($data, 'id = '.$id);    		
    	}
    	
    	return true;    	
	}
	
   


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }
    
    public function topDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            case 'title':
            	$result[$key] = mb_substr($value, 0, 33).'...';
            	
            	break;            	
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }    

}
?>